
import { gameConfigHandle } from "core/config/gameConfig"
import { GameRouteData, localApi } from "core/utils/localApi"
import bagHandle from "core/handle/bagHandle"
import equipmentHandle from "core/handle/equipmentHandel"
import playerHandle from "core/handle/playerHandle"
import skillHandle from "core/handle/skillHandle"
import { BagType, OccType } from "core/typeDefine/typeDefine"
import utils from "core/utils/utils"
import { ItemInfoModel } from "core/model/itemInfoModel"
import { TreasureItemModel } from "core/model/treasureModel"
import talentHandle from "core/handle/talentHandle"
import adventureHandle from "core/handle/adventureHandle"
import petHandle from "core/handle/petHandle"
import { PlayerModel } from "core/model/playerModel"
import { EquipmentsLvlupModel, PartnerModel } from "core/model/partnerModel"
import gameUtils from "core/utils/gameUtils"
import { GodModel } from "core/model/godModel"
import { FrontModel } from "core/model/luckModel"
import Random from "core/utils/random"


class CheatRoute {
    //角色解锁初始化
    @localApi("cheatRoute")
    setPartnerInitial(gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        player.team.lockList = {}
        player.team.teamList = []
        return { err: 0 }
    }
    //单角色上阵
    @localApi("cheatRoute")
    setOnePartnerOn(id: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        player.team.teamList = [id]
        player.team.lockList[id] = true
        return { err: 0 }
    }
    //解锁角色
    @localApi("cheatRoute")
    unlockPartner(id: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        if (id == null) {
            gameConfigHandle.gameConfig.recruit.recruitList.forEach(id => {
                player.team.lockList[id] = true
            })
        } else {
            player.team.lockList[id] = true
        }

        return { err: 0 }
    }
    @localApi("cheatRoute")
    setTradeLvl(lvl: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let trade = player.luck.trade
        trade.exp = 0
        let lvlMax = gameConfigHandle.gameConfig.luck.tradeLvlMax
        if (lvl > lvlMax) {
            lvl = lvlMax
        }
        trade.lvl = lvl
        return { err: 0 }
    }
    @localApi("cheatRoute")
    addFrontLvl(amount: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        player.luck.front.lvl += amount
        let lvlMax = gameConfigHandle.gameConfig.luck.frontLvlMax
        if (player.luck.front.lvl > lvlMax) {
            player.luck.front.lvl = lvlMax
        }
        player.luck.front.lvlMax = player.luck.front.lvl
        return { err: 0 }
    }
    @localApi("cheatRoute")
    addFrontScore(amount: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        player.itemInfo.frontScore += amount
        return { err: 0 }
    }
    @localApi("cheatRoute")
    resetFront(gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        player.luck.front = new FrontModel()
        return { err: 0 }
    }
    @localApi("cheatRoute")
    resetGod(gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        player.god = new GodModel()
        return { err: 0 }
    }
    @localApi("cheatRoute")
    wbossHurtMax(hurt: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        player.adventure.bossMapData.reduceBossHpMax = hurt
        return { err: 0 }
    }
    @localApi("cheatRoute")
    petAltarReset(gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        player.altarPets.altarAddAmount = 0
        player.altarPets.altarLvl = 0
        return { err: 0 }
    }
    @localApi("cheatRoute")
    petAltarUnlock(gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        player.altarPets.altarAddAmount = 11
        return { err: 0 }
    }
    @localApi("cheatRoute")
    heroCheat(type: string, tid: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        if (type == 'lock') {
            if (player.team.lockList[tid]) {
                player.team.lockList[tid] = false
            } else {
                player.team.lockList[tid] = true
            }
        } else if (type == 'resetStar') {
            let partner: PartnerModel = player.team.recruitList[tid]
            if (!partner) {
                player.team.recruitList[tid] = new PartnerModel()
                partner = player.team.recruitList[tid]
                partner.tid = tid
            }
            partner.starLvl = 0
        } else if (type == 'starLvlUp') {
            let partnerConfig = gameConfigHandle.gameConfig.partner
            let partner: PartnerModel = player.team.recruitList[tid]
            if (!partner) {
                player.team.recruitList[tid] = new PartnerModel()
                partner = player.team.recruitList[tid]
                partner.tid = tid
            }
            if (partner.starLvl < partnerConfig.starMaxLvl) {
                partner.starLvl++
            }
        }
        return { err: 0 }
    }
    @localApi("cheatRoute")
    resetMarketBuyTimes(gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        player.market.freeTimes = 0
        player.market.refreshTimes = 0
        player.market.buyDayAmount = 0
        return { err: 0 }
    }
    @localApi("cheatRoute")
    addPet(gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        let pet = petHandle.create(null, 1, random, {})
        bagHandle.addItemComponent(player.petBag, pet)
        return { err: 0 }
    }
    @localApi("cheatRoute")
    cheatPlayer(gameRouteData?: GameRouteData) {
        let { player } = gameRouteData
        // player.baseInfo.mailList = []
        return { err: 0 }
    }

    @localApi("cheatRoute")
    createEquipment(opts: any, gameRouteData?: GameRouteData) {
        let { player, time, dayTime, random } = gameRouteData
        let item = equipmentHandle.create(player, null, random, opts)
        let err = bagHandle.addItemComponent(player.equipmentBag, item)
        return { err: 0, data: item }
    }

    @localApi("cheatRoute")
    clearBag(bagType: BagType, gameRouteData?: GameRouteData) {
        let { player, } = gameRouteData
        if (bagType == BagType.Bag) {
            bagHandle.clear(player.bag)
        } else if (bagType == BagType.Equipment) {
            bagHandle.clear(player.equipmentBag)
        } else if (bagType == BagType.Skill) {
            bagHandle.clear(player.skillBag)
        } else if (bagType == BagType.Pet) {
            bagHandle.clear(player.petBag)
        } else if (bagType == null) {
            utils.keys(player.itemInfo).forEach(itemId => {
                player.itemInfo[itemId] = 0
            })
        }
        return { err: 0 }
    }
    @localApi("cheatRoute")
    clearEquipment(gameRouteData?: GameRouteData) {
        let { player, } = gameRouteData
        let equipmentConfig = gameConfigHandle.gameConfig.equipment
        let partnerConfig = gameConfigHandle.gameConfig.partner
        let teamEquipment = player.teamEquipment
        partnerConfig.occTypeList.forEach(occType => {
            let equipments = teamEquipment.equipmentList[occType][0]
            equipmentConfig.equipmentListType.map(equipmentType => {
                equipments[equipmentType] = null
            })
        })
        return { err: 0 }
    }

    @localApi("cheatRoute")
    partnerMaxLvl(type: string, isMax: boolean, gameRouteData?: GameRouteData) {
        let { player,random } = gameRouteData
        let equipmentConfig = gameConfigHandle.gameConfig.equipment
        let partnerConfig = gameConfigHandle.gameConfig.partner
        let skillConfig = gameConfigHandle.gameConfig.skill
        let researchConfig = gameConfigHandle.gameConfig.research
        let treasureConfig = gameConfigHandle.gameConfig.treasure
        let formationConfig = gameConfigHandle.gameConfig.formation
        let needConfig = gameConfigHandle.gameConfig.need
        let altarConfig = gameConfigHandle.gameConfig.altar
        let getData = {}
        if (type == null || type == 'lvl') {
            if (isMax) {
                player.altar.lvl = partnerConfig.maxLvl
                let totalNeedData = gameUtils.getNeedResource(player, needConfig.partnerLvlupNeed, 1, 1, partnerConfig.maxLvl)
                totalNeedData.needNodes.forEach(item => {
                    let itemID = item[0]
                    let itemNum = item[1]
                    getData[itemID] = (getData[itemID] || 0) + itemNum
                })
            } else {
                player.altar.lvl = 0
            }
        }

        let recruitConfig = gameConfigHandle.gameConfig.recruit
        recruitConfig.recruitList.forEach(partnerTID => {
            let partner = player.team.recruitList[partnerTID]
            if (!partner) {
                partner = new PartnerModel()
                partner.tid = partnerTID
                player.team.recruitList[partnerTID] = partner
            }
            if (type == null || type == 'starLvl') {
                if (isMax) {
                    partner.starLvl = partnerConfig.starMaxLvl
                    let totalNeedData = gameUtils.getNeedResource(player, needConfig.heroStarLvlUp[partner.tid], 1, 1, partnerConfig.starMaxLvl)
                    totalNeedData.needNodes.forEach(item => {
                        let itemID = item[0]
                        let itemNum = item[1]
                        getData[itemID] = (getData[itemID] || 0) + itemNum
                    })
                } else {
                    partner.starLvl = 0
                }
            }
        })
        if (type == null || type == 'equipmentsStr') {
            let list = [OccType.Warrior, OccType.MainTank, OccType.Wizard, OccType.Assassin, OccType.Archer, OccType.Assistant]
            recruitConfig.recruitList.forEach(partnerTID => {
                let partner = player.team.recruitList[partnerTID]
                if (!partner) {
                    partner = new PartnerModel()
                    partner.tid = partnerTID
                    player.team.recruitList[partnerTID] = partner
                }
                equipmentConfig.equipmentListType.map(equipmentType => {
                    if (isMax) {
                        let totalNeedData = gameUtils.getNeedResource(player, needConfig.equipmentStrNeed, 0, 1, partnerConfig.maxLvl)
                        totalNeedData.needNodes.forEach(item => {
                            let itemID = item[0]
                            let itemNum = item[1]
                            getData[itemID] = (getData[itemID] || 0) + itemNum
                        })
                        partner.equipmentsStr[equipmentType] = partnerConfig.maxLvl
                    } else {
                        partner.equipmentsStr[equipmentType] = 0
                    }
                })
            })
        }
        if (type == null || type == 'packageAttributes') {
            let lvlMax = equipmentConfig.packageAttributesLvlMax
            equipmentConfig.packageAttributesList.map(id => {
                if (isMax) {
                    let totalNeedData = gameUtils.getNeedResource(player, needConfig.packageAttributes[id], 1, 1, lvlMax)
                    totalNeedData.needNodes.forEach(item => {
                        let itemID = item[0]
                        let itemNum = item[1]
                        getData[itemID] = (getData[itemID] || 0) + itemNum
                    })
                    player.packageAttributesLvls[id] = lvlMax
                } else {
                    player.packageAttributesLvls[id] = 1
                }
            })
        }
        if (type == null || type == 'altarSkills') {
            let altarConfig = gameConfigHandle.gameConfig.altar
            altarConfig.skillListLvl.forEach((_, index) => {
                if (isMax) {
                    let totalNeedData = gameUtils.getNeedResource(player, needConfig.altarSkillLvlUp[index], 0, 1, altarConfig.skillMaxLvl)
                    totalNeedData.needNodes.forEach(item => {
                        let itemID = item[0]
                        let itemNum = item[1]
                        getData[itemID] = (getData[itemID] || 0) + itemNum
                    })
                    player.altar.skillsLvl[index] = altarConfig.skillMaxLvl
                } else {
                    player.altar.skillsLvl[index] = 0
                }
            })
        }
        player.team.teamList.forEach(tid => {
            if (tid) {
                let partner = player.team.recruitList[tid]
                if (partner) {
                    if (type == null || type == 'lvl') {
                        if (isMax) {
                            let totalNeedData = gameUtils.getNeedResource(player, needConfig.partnerLvlupNeed, 0, 1, partnerConfig.maxLvl)
                            totalNeedData.needNodes.forEach(item => {
                                let itemID = item[0]
                                let itemNum = item[1]
                                getData[itemID] = (getData[itemID] || 0) + itemNum
                            })
                            partner.lvl = partnerConfig.maxLvl
                        } else {
                            partner.lvl = 1
                        }
                    }
                }
            }
        })

        if (type == null || type == 'immunes') {
            researchConfig.immunes.forEach(immunes => {
                immunes.forEach((_, index) => {
                    if (isMax) {
                        let lvlMax = researchConfig.immuneMaxLvl
                        let totalNeedData = gameUtils.getNeedResource(player, needConfig.immuneUpNeed, index * 2, 1, lvlMax + index * 2)
                        totalNeedData.needNodes.forEach(item => {
                            let itemID = item[0]
                            let itemNum = item[1]
                            getData[itemID] = (getData[itemID] || 0) + itemNum
                        })

                        player.research.immuneList[index] = lvlMax
                    } else {
                        player.research.immuneList[index] = 0
                    }
                })
            })
        }
        if (type == null || type == 'immunesStar') {
            researchConfig.immunes.forEach(immunes => {
                immunes.forEach((_, index) => {
                    if (isMax) {
                        let starMax = researchConfig.immuneMaxStar
                        let totalNeedData = gameUtils.getNeedResource(player, needConfig.immuneStarUpNeed, index * 2, 1, starMax + index * 2)
                        totalNeedData.needNodes.forEach(item => {
                            let itemID = item[0]
                            let itemNum = item[1]
                            getData[itemID] = (getData[itemID] || 0) + itemNum
                        })
                        player.research.immuneStarList[index] = starMax
                    } else {
                        player.research.immuneStarList[index] = 0
                    }
                })
            })
        }
        if (type == null || type == 'talent') {
            if (player.talent.list[player.talent.activeIndex]) {
                player.talent.list[player.talent.activeIndex].forEach(list => {
                    list.forEach(node => {
                        if (isMax) {
                            node.lvl = 5
                        } else {
                            node.lvl = 0
                        }
                    })
                })
            }
            let talentConfig = gameConfigHandle.gameConfig.talent
            if (isMax) {
                let totalNeedData = gameUtils.getNeedResource(player, needConfig.talentLvlup, 0, 1, talentConfig.maxLvl)
                totalNeedData.needNodes.forEach(item => {
                    let itemID = item[0]
                    let itemNum = item[1]
                    getData[itemID] = (getData[itemID] || 0) + itemNum
                })
                player.talent.lvl = talentConfig.maxLvl
            } else {
                player.talent.lvl = 0
            }
        }
        if (type == null || type == 'treasure') {
            treasureConfig.list.forEach(id => {
                if (!player.treasure.list[id]) {
                    player.treasure.list[id] = new TreasureItemModel()
                }
                if (isMax) {
                    let treasureItemConfig = gameConfigHandle.configByID[id]
                    let totalNeedData = gameUtils.getNeedResource(player, treasureItemConfig.need, 0, 1, treasureConfig.maxLvl)
                    totalNeedData.needNodes.forEach(item => {
                        let itemID = item[0]
                        let itemNum = item[1]
                        getData[itemID] = (getData[itemID] || 0) + itemNum
                    })
                    player.treasure.list[id].lvl = treasureConfig.maxLvl
                } else {
                    player.treasure.list[id].lvl = 0
                }
            })
        }
        if (type == null || type == 'treasureStar') {
            treasureConfig.list.forEach(id => {
                if (!player.treasure.list[id]) {
                    player.treasure.list[id] = new TreasureItemModel()
                }
                if (isMax) {
                    let treasureItemConfig = gameConfigHandle.configByID[id]
                    let totalNeedData = gameUtils.getNeedResource(player, treasureItemConfig.starNeed, 0, 1, treasureConfig.starMax)
                    totalNeedData.needNodes.forEach(item => {
                        let itemID = item[0]
                        let itemNum = item[1]
                        getData[itemID] = (getData[itemID] || 0) + itemNum
                    })
                    player.treasure.list[id].star = treasureConfig.starMax
                } else {
                    player.treasure.list[id].star = 0
                }
            })
        }
        if (type == null || type == 'formation') {
            let totalLvl = 0
            formationConfig.formationList.forEach((id, index) => {
                if (isMax) {
                    let totalNeedData = gameUtils.getNeedResource(player, needConfig.formationLvlUp[index], 0, 1, formationConfig.formationMaxLvl)
                    totalNeedData.needNodes.forEach(item => {
                        let itemID = item[0]
                        let itemNum = item[1]
                        getData[itemID] = (getData[itemID] || 0) + itemNum
                    })
                    player.formation.formationLvl[index] = formationConfig.formationMaxLvl
                    totalLvl += formationConfig.formationMaxLvl
                } else {
                    player.formation.formationLvl[index] = 0
                }
            })
            player.formation.lvl = totalLvl
        }
        if (type == null || type == 'altarLvl') {
            let totalLvl = 0
            altarConfig.attriTypeList.forEach((attriItem, index) => {
                let attriName = attriItem.name
                if (isMax) {
                    let totalNeedData = gameUtils.getNeedResource(player, needConfig.altarStrNeed[index], 0, 1, altarConfig.altarStrMaxLvl)
                    totalNeedData.needNodes.forEach(item => {
                        let itemID = item[0]
                        let itemNum = item[1]
                        getData[itemID] = (getData[itemID] || 0) + itemNum
                    })
                    player.altar.attriLvls[index] = altarConfig.altarStrMaxLvl
                    totalLvl += altarConfig.altarStrMaxLvl
                } else {
                    player.altar.attriLvls[index] = 0
                }
            })
            player.altar.totalAttriLvl = totalLvl
        }
        if (type == null || type == 'petAltar') {
            if (isMax) {
                let lvlMax = gameConfigHandle.gameConfig.pet.altarLvlMax
                let totalNeedData = gameUtils.getNeedResource(player, needConfig.petAltarlvlUp, 0, 1, lvlMax)
                totalNeedData.needNodes.forEach(item => {
                    let itemID = item[0]
                    let itemNum = item[1]
                    getData[itemID] = (getData[itemID] || 0) + itemNum
                })
                player.altarPets.altarLvl = lvlMax
            } else {
                player.altarPets.altarLvl = 0
            }
        }
        if (type == null || type == 'god') {
            if (isMax) {
                let talentConfig = gameConfigHandle.gameConfig.talent
                let lvlMax = talentConfig.godNodeLvlMax
                gameConfigHandle.gameConfig.talent.godIndexs.forEach(i => {
                    let nodeTypeLvl = talentConfig.godNodeTypeLvl[i]
                    let totalNeedData = gameUtils.getNeedResource(player, needConfig.godLvlup[nodeTypeLvl], 0, 1, lvlMax)
                    totalNeedData.needNodes.forEach(item => {
                        let itemID = item[0]
                        let itemNum = item[1]
                        getData[itemID] = (getData[itemID] || 0) + itemNum
                    })
                    player.god.nodeLvls[i] = lvlMax
                })
            } else {
                player.god.nodeLvls = {}
            }
        }
        if (type == null || type == 'godStr') {
            if (isMax) {
                let lvlMax = gameConfigHandle.gameConfig.talent.godStrLvlMax
                let totalNeedData = gameUtils.getNeedResource(player, needConfig.godStrLvlup, 0, 1, lvlMax)
                totalNeedData.needNodes.forEach(item => {
                    let itemID = item[0]
                    let itemNum = item[1]
                    getData[itemID] = (getData[itemID] || 0) + itemNum
                })
                player.god.strLvl = lvlMax
            } else {
                player.god.strLvl = 0
            }
        }
        if (type == null || type == 'worship') {
            if (isMax) {
                let lvlMax = gameConfigHandle.gameConfig.luck.worshipLvlMax
                player.luck.worship.lvl = lvlMax
            } else {
                player.luck.worship.lvl = 0
                player.luck.worship.exp = 0
                player.itemInfo.worship = 0
            }
        }
        if (type == null || type == 'pet') {
            let maxLvl = gameConfigHandle.gameConfig.pet.maxLvl
            //祭坛里的宠物
            player.altarPets.petsInAltar.forEach(pet => {
                if (pet) {
                    if (isMax) {
                        let totalNeedData = gameUtils.getNeedResource(player, needConfig.petLvlUpNeed, 0, 1, maxLvl)
                        totalNeedData.needNodes.forEach(item => {
                            let itemID = item[0]
                            let itemNum = item[1]
                            getData[itemID] = (getData[itemID] || 0) + itemNum
                        })
                        pet.pet.lvl = maxLvl
                    } else {
                        pet.pet.lvl = 0
                    }
                }
            })
            //英雄携带的宠物
            player.team.teamList.forEach(id => {
                if (id) {
                    let pet = playerHandle.getPartnerByIndexID(player, id).pet
                    if (pet) {
                        if (isMax) {
                            let totalNeedData = gameUtils.getNeedResource(player, needConfig.petLvlUpNeed, 0, 1, maxLvl)
                            totalNeedData.needNodes.forEach(item => {
                                let itemID = item[0]
                                let itemNum = item[1]
                                getData[itemID] = (getData[itemID] || 0) + itemNum
                            })
                            pet.pet.lvl = maxLvl
                        } else {
                            pet.pet.lvl = 0
                        }
                    }
                }
            })
        }
        if (type == null || type == 'starSkill') {
            gameConfigHandle.gameConfig.partner.redHeroList.forEach(id => {
                let partner = playerHandle.getPartnerByIndexID(player, id)
                if (isMax) {
                    partner.starSkillUnlock = [0, 1, 2, 3]
                } else {
                    partner.starSkillUnlock = []
                }
            })
        }
        if (type == null || type == 'friendship') {
            if (isMax) {
                let maxLvl = gameConfigHandle.gameConfig.friendship.maxLvl
                gameConfigHandle.gameConfig.friendship.lists.forEach((list, index) => {
                    list.forEach(id => {
                        let itemConfig = gameConfigHandle.configByID[id]
                        if (itemConfig) {
                            let totalNeedData = gameUtils.getNeedResource(player, needConfig.friendshipLvlUp[index], 0, 1, maxLvl)
                            totalNeedData.needNodes.forEach(item => {
                                let itemID = item[0]
                                let itemNum = item[1]
                                getData[itemID] = (getData[itemID] || 0) + itemNum
                            })
                            player.friendship.friendshipLvls[id] = maxLvl
                            player.friendship.subAttris[id] = utils.getRandomArrayNoRepeat(gameConfigHandle.configByID[id].subAttris, 1, new Random())
                            player.friendship.attris[id] = utils.getRandomArrayNoRepeat(gameConfigHandle.configByID[id].attris, 2, new Random())
                        }
                    })
                })
            } else {
                player.friendship.friendshipLvls = {}
                player.friendship.friendshipSkills = {}
                player.friendship.attris = {}
                player.friendship.subAttris = {}
                player.friendship.disciples = {}
            }
        }
        if (type == null || type == 'spirit') {
            if (isMax) {
                let maxLvl = gameConfigHandle.gameConfig.spirit.maxLvl
                let spiritConfig = gameConfigHandle.gameConfig.spirit
                gameConfigHandle.gameConfig.partner.occTypeList.forEach(occType => {
                    gameConfigHandle.gameConfig.spirit.spiritLists[occType].forEach(id => {
                        let totalNeedData = gameUtils.getNeedResource(player, needConfig.spiritLvlup[occType], 0, 1, maxLvl)
                        totalNeedData.needNodes.forEach(item => {
                            let itemID = item[0]
                            let itemNum = item[1]
                            getData[itemID] = (getData[itemID] || 0) + itemNum
                        })
                        player.spirit.spiritLvls[id] = maxLvl
                    })
                    player.spirit.totalLvlAttris[occType] = utils.getRandomArrayNoRepeat(spiritConfig.spiritTotalLvlAttris[occType], 2, random)
                })
                // gameConfigHandle.gameConfig.friendship.lists.forEach((list, index) => {
                //     list.forEach(id => {
                //         let itemConfig = gameConfigHandle.configByID[id]
                //         if (itemConfig) {
                //             let totalNeedData = gameUtils.getNeedResource(player, needConfig.friendshipLvlUp[index], 0, 1, maxLvl)
                //             totalNeedData.needNodes.forEach(item => {
                //                 let itemID = item[0]
                //                 let itemNum = item[1]
                //                 getData[itemID] = (getData[itemID] || 0) + itemNum
                //             })
                //             player.friendship.friendshipLvls[id] = maxLvl
                //             player.friendship.subAttris[id] = utils.getRandomArrayNoRepeat(gameConfigHandle.configByID[id].subAttris, 1, new Random())
                //             player.friendship.attris[id] = utils.getRandomArrayNoRepeat(gameConfigHandle.configByID[id].attris, 2, new Random())
                //         }
                //     })
                // })
            } else {
                player.spirit.spiritLvls = {}
                player.spirit.totalLvlAttris = {}
            }
        }
        adventureHandle.calcTreasureAdventureAdd(player)
        equipmentHandle.calcEquipmentAddGoldPer(player)
        return { err: 0, data: getData }
    }

    @localApi("cheatRoute")
    partnerResetLvl(gameRouteData?: GameRouteData) {
        let { player, } = gameRouteData
        let equipmentConfig = gameConfigHandle.gameConfig.equipment
        let partnerConfig = gameConfigHandle.gameConfig.partner
        let skillConfig = gameConfigHandle.gameConfig.skill
        let researchConfig = gameConfigHandle.gameConfig.research
        let treasureConfig = gameConfigHandle.gameConfig.treasure
        player.team.list.forEach(partner => {
            if (partner) {
                partner.lvl = 1
                equipmentConfig.equipmentListType.map(equipmentType => {
                    partner.equipmentsStr[equipmentType] = 0
                })
            }

        })
        skillConfig.skillList.forEach(skillID => {
            player.skillLvls[skillID] = 1
        })
        skillConfig.lSkill.forEach(skillID => {
            player.lskillLvls[skillID] = 1
        })
        researchConfig.immunes.forEach(immunes => {
            immunes.forEach(immuneid => {
                player.research.immuneList[immuneid] = 0
            })
        })
        player.talent.list[player.talent.activeIndex].forEach(list => {
            list.forEach(node => {
                node.lvl = 0
            })
        })
        treasureConfig.list.forEach(id => {
            player.treasure.list[id] = new TreasureItemModel()
        })
        let magicMachineConfig = gameConfigHandle.gameConfig.magicMachine

        return { err: 0 }
    }
    @localApi("cheatRoute")
    addNewEquipmentLvls(equipid, lvl, gameRouteData?: GameRouteData) {

    }

    @localApi("cheatRoute")
    resetTalent(gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        talentHandle.create(player, 0, random)
        talentHandle.create(player, 1, random)
        talentHandle.create(player, 2, random)
        adventureHandle.calcTalentResoureAdd(player)
        return { err: 0 }
    }

    @localApi("cheatRoute")
    resetGemTeamLvl(gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData
        player.gemTeamLvl = 0
        return { err: 0 }
    }

    @localApi("cheatRoute")
    resetImmunes(gameRouteData?: GameRouteData) {
        let { player, } = gameRouteData
        let researchConfig = gameConfigHandle.gameConfig.research
        researchConfig.immunes.forEach(immunes => {
            immunes.forEach(immuneid => {
                player.research.immuneList[immuneid] = 0
            })
        })
        player.research.equipmentLvls.forEach((value, index, array) => {
            array[index] = 0
        })
        return { err: 0 }
    }

    @localApi("cheatRoute")
    resetTreasure(gameRouteData?: GameRouteData) {
        let { player, } = gameRouteData
        let treasureConfig = gameConfigHandle.gameConfig.treasure
        treasureConfig.list.forEach(id => {
            player.treasure.list[id] = new TreasureItemModel()
        })
        return { err: 0 }
    }



    @localApi("cheatRoute")
    addItem(type: string | number, value: number, gameRouteData?: GameRouteData) {
        let { player, } = gameRouteData
        if (type == 0) {
            utils.keys(new ItemInfoModel()).map((e => {
                let name = e
                player.itemInfo[e] += value
            }))
            utils.entries(gameConfigHandle.gameConfig.items).map((item => {
                let itemID = Number(item[0])
                let itemConfig: any = item[1]
                if (itemConfig.itemSubType < 50 || itemConfig.subType < 50) {
                    let res = bagHandle.addItem(player.bag, itemID, value)
                }
            }))
        } else if (type == 1) {
            utils.entries(gameConfigHandle.gameConfig.items).map((item => {
                let itemID = Number(item[0])
                let itemConfig: any = item[1]
                if (itemConfig.itemSubType < 50 || itemConfig.subType < 50) {
                    let res = bagHandle.addItem(player.bag, itemID, value)
                }
            }))
        } else if (type == 2) {
            utils.keys(new ItemInfoModel()).map((e => {
                let name = e
                player.itemInfo[e] += value
            }))
        } else if (typeof type == "string") {
            player.itemInfo[type] += value
        } else {
            bagHandle.addItem(player.bag, type, value)
        }
        return { err: 0 }
    }

    @localApi("cheatRoute")
    resetDay(gameRouteData?: GameRouteData) {
        let { player, time, dayTime } = gameRouteData
        playerHandle.resetDay(player, time, dayTime)
        return { err: 0 }
    }
    @localApi("cheatRoute")
    resetNormalTask(gameRouteData?: GameRouteData) {
        let { player, time, dayTime } = gameRouteData
        player.task.list = {}
        player.task.doneAmount = 0
        return { err: 0 }
    }

    @localApi("cheatRoute")
    addSkill({ skillId, quality, lSkills }, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData

        let item = skillHandle.createBook(random, { skillId, quality, lSkills })
        let res = bagHandle.addItemComponent(player.skillBag, item)
        return { err: 0, data: item }
    }

    @localApi("cheatRoute")
    addoffline(minute: number, gameRouteData?: GameRouteData) {
        let { player, time } = gameRouteData
        player.adventure.getResourceTime -= minute * 60 * 1000
        player.adventure.offlineTime -= minute * 60 * 1000
        return { err: 0 }
    }

    @localApi("cheatRoute")
    resetSkill(gameRouteData?: GameRouteData) {
        let { player, time } = gameRouteData
        player.skillLvls = {}
        return { err: 0 }
    }
    @localApi("cheatRoute")
    skillLvUp(lvl: number, gameRouteData?: GameRouteData) {
        let { player, time } = gameRouteData
        let skillConfig = gameConfigHandle.gameConfig.skill
        skillConfig.skillList.forEach(skillID => {
            if (!player.skillLvls[skillID]) {
                player.skillLvls[skillID] = 0
            }
            player.skillLvls[skillID] += lvl
            if (player.skillLvls[skillID] >= skillConfig.skillMaxlvl) {
                player.skillLvls[skillID] = skillConfig.skillMaxlvl
            }
        })
        return { err: 0 }
    }
    @localApi("cheatRoute")
    resetLSkill(gameRouteData?: GameRouteData) {
        let { player, time } = gameRouteData
        player.lskillLvls = {}
        return { err: 0 }
    }
    @localApi("cheatRoute")
    resetGold(gameRouteData?: GameRouteData) {
        let { player, time } = gameRouteData
        player.itemInfo.gold = 0
        return { err: 0 }
    }
    @localApi("cheatRoute")
    resetDiamond(gameRouteData?: GameRouteData) {
        let { player, time } = gameRouteData
        player.itemInfo.diamond = 0
        return { err: 0 }
    }
    @localApi("cheatRoute")
    resetExp(gameRouteData?: GameRouteData) {
        let { player, time } = gameRouteData
        player.itemInfo.exp = 0
        return { err: 0 }
    }
    @localApi("cheatRoute")
    resetVip(gameRouteData?: GameRouteData) {
        let { player, time } = gameRouteData
        player.itemInfo.vipExp = 0
        return { err: 0 }
    }

    @localApi("cheatRoute")
    addMapProgress(value, type, gameRouteData?: GameRouteData) {
        let { player, time } = gameRouteData
        if (type == 1) {
            player.adventure.floor += value
            player.adventure.mapProgress[player.adventure.mapID] = player.adventure.floor
        } else if (type == 2) {
            let mapConfig = gameConfigHandle.gameConfig.map
            let instanceList = mapConfig.instanceList
            instanceList.forEach(mapID => {
                let mapConfig = gameConfigHandle.configByID[mapID]
                if (!player.adventure.mapProgress[mapID]) {
                    player.adventure.mapProgress[mapID] = 0
                }
                player.adventure.mapProgress[mapID] += value
                if (player.adventure.mapProgress[mapID] > mapConfig.maxFloor) {
                    player.adventure.mapProgress[mapID] = mapConfig.maxFloor
                }
                if (player.adventure.mapProgress[mapID] < 1) {
                    player.adventure.mapProgress[mapID] = 1
                }
            })
        }
        return { err: 0 }
    }
    @localApi("cheatRoute")
    gotoMap(mapID, gameRouteData?: GameRouteData) {
        let { player, time } = gameRouteData
        player.adventure.mapID = mapID
        player.adventure.floor = 0
        return { err: 0 }
    }

    @localApi("cheatRoute")
    setMapProgressMax(mapID: number, gameRouteData?: GameRouteData) {
        let { player, time } = gameRouteData

        let mapConfig = gameConfigHandle.gameConfig.map
        if (mapID) {
            player.adventure.mapProgress = {}
            mapConfig.mapList.forEach(id => {
                if (id < mapID) {
                    let mapConfig = gameConfigHandle.configByID[id]
                    player.adventure.mapProgress[id] = mapConfig.maxFloor
                }
            })
            player.adventure.mapProgress[mapID] = 4
            player.adventure.mapID = mapID
            player.adventure.floor = 4
        } else {
            player.adventure.mapProgress = {}
            mapConfig.mapList.forEach(id => {
                let mapConfig = gameConfigHandle.configByID[id]
                player.adventure.mapProgress[id] = mapConfig.maxFloor
            })
        }
        return { err: 0 }
    }

    @localApi("cheatRoute")
    setMapExploreProgressMax(mapID: number, gameRouteData?: GameRouteData) {
        let { player, time } = gameRouteData
        player.adventure.mapExploreProgress = {}
        let mapConfig = gameConfigHandle.gameConfig.map
        if (mapID) {
            let mapConfig = gameConfigHandle.configByID[mapID]
            player.adventure.mapExploreProgress[mapID] = mapConfig.maxExplore
        } else {
            mapConfig.mapList.forEach(mapID => {
                let mapConfig = gameConfigHandle.configByID[mapID]
                player.adventure.mapExploreProgress[mapID] = mapConfig.maxExplore
            })
        }
        return { err: 0 }
    }

    @localApi("cheatRoute")
    clearMapProgress(mapID: number, gameRouteData?: GameRouteData) {
        let { player, time } = gameRouteData
        if (mapID) {
            player.adventure.mapProgress[mapID] = 0
            player.adventure.mapExploreProgress[mapID] = 0
        } else {
            player.adventure.floor = 1
            player.adventure.mapID = gameConfigHandle.gameConfig.map.adventureMapID
            let mapConfig = gameConfigHandle.gameConfig.map
            let instanceList = mapConfig.instanceList
            instanceList.forEach(mapID => {
                let mapConfig = gameConfigHandle.configByID[mapID]
                player.adventure.mapProgress[mapID] = 1

            })
            player.adventure.mapProgress = {}
            player.adventure.mapExploreProgress = {}
        }

        return { err: 0 }
    }

    @localApi("cheatRoute")
    resetPartnerLvl(gameRouteData?: GameRouteData) {
        let { player, time } = gameRouteData
        let partner = playerHandle.getPartner(player)
        partner.lvl = 1
        let partnerConfig = gameConfigHandle.gameConfig.partner
        let baseAttris = utils.keys(partnerConfig.baseAttri).slice(0)
        baseAttris.forEach(type => {
            partner.baseAttris[type] = 0
        })
        partner.baseAttris[utils.getOneFromArray(baseAttris)] = 1
        partner.baseAttris.lvl = 0
        return { err: 0 }
    }
    @localApi("cheatRoute")
    resetNormalFloor(gameRouteData?: GameRouteData) {
        let { player, time } = gameRouteData
        player.adventure.floor = 1
        return { err: 0 }
    }
    @localApi("cheatRoute")
    resetInstance(gameRouteData?: GameRouteData) {
        let { player, time } = gameRouteData
        gameConfigHandle.gameConfig.adventure.intanceMapList.forEach(id => {
            player.adventure.mapProgress[id] = 0
        })
        return { err: 0 }
    }
    @localApi("cheatRoute")
    resetAbyssProgress(gameRouteData?: GameRouteData) {
        let { player, time } = gameRouteData
        player.adventure.mapProgress[300201] = 0
        return { err: 0 }
    }

    @localApi("cheatRoute")
    resetPlayer(gameRouteData?: GameRouteData) {
        let { time, isMonthVip, isYearVip } = gameRouteData
        let player = playerHandle.createPlayer(time)
        playerHandle.initPlayer(player, isMonthVip, isYearVip)
        gameRouteData.player = player
        return { err: 0 }
    }

    @localApi("cheatRoute")
    copyPlayer(data: any, gameRouteData?: GameRouteData) {
        let { time, isMonthVip, isYearVip } = gameRouteData
        let player = new PlayerModel(data)
        playerHandle.initPlayer(player, isMonthVip, isYearVip)
        gameRouteData.player = player
        return { err: 0 }
    }

    @localApi("cheatRoute")
    createPet(petID: number, lvl: number, quality: number, elements: number[], star: number, gameRouteData?: GameRouteData) {
        let { random, player } = gameRouteData
        let item = petHandle.create(petID, 10, random, { lvl, quality, elements, star })
        bagHandle.addItemComponent(player.petBag, item)
        return { err: 0, data: item }
    }

    @localApi("cheatRoute")
    unlockHero(gameRouteData?: GameRouteData) {
        let { random, player } = gameRouteData
        let recruitConfig = gameConfigHandle.gameConfig.recruit
        recruitConfig.recruitList.forEach(heroID => {
            player.team.lockList[heroID] = true
        })
        return { err: 0 }
    }
}
let cheatRoute: CheatRoute = new CheatRoute()

export default cheatRoute
